// -*- go -*-
//
// Copyright (c) 2020 Markku Rossi
//
// All rights reserved.
//

package math

// Exp computes b**e mod |m| (i.e. the sign of m is ignore).
func Exp(b, e, m uint) uint {
	rType := make(uint, size(m))
	mType := make(uint, size(m)*2)

	var r mType = 1

	for i := size(e) - 1; i >= 0; i = i - 1 {
		r = r * r % mType(m)
		if e>>i&1 != 0 {
			r = r * mType(b) % mType(m)
		}
	}

	return rType(r)
}
